﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
//using System.Linq;
using System.Text;
//using System.Threading.Tasks;
using System.Windows.Forms;


namespace TRicheControl.WinformControl
{
    using TRicheControl.Comm;
    using WindowsAPI;

    [ToolboxBitmap(typeof(TextBox))]
    public partial class RicheTextBox : TextBox
    {
        private const int WM_PAINT = 0x000F;

        public RicheTextBox()
        {
            InitializeComponent();
            this.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.SetStyle(ControlStyles.OptimizedDoubleBuffer |
                          ControlStyles.ResizeRedraw |
                          ControlStyles.AllPaintingInWmPaint, true);
        }

        private Graphics mGraphics;
        public Graphics TextBoxGraphics
        {
            get 
            {
                if (base.IsDisposed)
                {
                    return null;
                }
                if (mGraphics == null)
                {
                    mGraphics = base.CreateGraphics();
                }
                return mGraphics;
            }
        }

        private string mStringFormate = "";

        public string StringFormate
        {
            get
            {
                return mStringFormate;
            }
            set
            {
                mStringFormate = value;
            }
        }

        private string mWatermark;

        /// <summary>
        /// 水印
        /// </summary>
        [Description("设置水印")]
        public string Watermark
        {
            get
            {
                return mWatermark;
            }
            set
            {
                mWatermark = value;
            }
        }

        private Font mWatermarkFont;
        /// <summary>
        /// 水印字体
        /// </summary>
        [Description("设置水印字体")]
        public Font WatermarkFont
        {
            get
            {
                return mWatermarkFont;
            }
            set
            {
                mWatermarkFont = value;
            }
        }

        protected override void WndProc(ref Message m)
        {
            base.WndProc(ref m);
            if (m.Msg == 0xf || m.Msg == 0x133)//m.Msg == WM_PAINT)
            {
                IntPtr hDC = NativeMethods.GetWindowDC(m.HWnd);
                if (hDC.ToInt32() == 0)
                    return;
                Graphics g = Graphics.FromHdc(hDC);
                DrawTextBox(g);
                NativeMethods.ReleaseDC(m.HWnd, hDC);
            }
        }

        private void DrawTextBox(Graphics g)
        {
            Rectangle drawRect = new Rectangle(0, 0, this.Width - 1, this.Height - 1);
            if (this.Enabled)
            {
                GraphicsStyle.Styles.TextBoxStyle.DrawEnableBox(g, drawRect);
            }
            else
            {
                GraphicsStyle.Styles.TextBoxStyle.DrawUnEnableBox(g, drawRect);
            }
            if (!this.Focused && this.Text.Length < 1)
            {
                GraphicsStyle.Styles.TextBoxStyle.DrawWatermark(Watermark, this.WatermarkFont, g, drawRect);
            }
        }

        protected override void OnGotFocus(EventArgs e)
        {
            Rectangle drawRect = new Rectangle(0, 0, this.Width, this.Height);
            if (this.Enabled)
            {
                GraphicsStyle.Styles.TextBoxStyle.DrawGotFouseEnableBox(this.TextBoxGraphics, drawRect);
            }
            else
            {
                GraphicsStyle.Styles.TextBoxStyle.DrawUnEnableBox(this.TextBoxGraphics, drawRect);
            }
            base.OnGotFocus(e);
        }

        protected override void OnTextChanged(EventArgs e)
        {
            try
            {
                if (this.Text.Length < 1)
                    return;
                if (mStringFormate != "")
                {
                    this.Text = string.Format(mStringFormate, this.Text);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            base.OnTextChanged(e);
        }
    }
}
